package com.xdd;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @author XDD
 * @project reptile-demo
 * @date 2020/4/20
 * @description 美图录：https://www.meitulu.com
 */
public class Meitulu {

    //图片保存路径
    private final static String rootPath = "F:\\meitulu";

    //目标网站
    private final static String WEBURL = "https://www.meitulu.com";

    //保存成功图片个数
    private static Long successCount = 0L;

    //保存失败图片个数
    private static Long failCount = 0L;



    public static void main(String[] args) throws Exception{
        for(int i=0;i<=21630;i++){
            //TODO 可以开启多线程，线程一：0~5630；线程二：5531~10000，依次类推
            parsetImgPageInfo("https://www.meitulu.com/item/"+i+".html",String.valueOf(i));
            System.out.println("保存成功: "+successCount+"， 失败："+failCount);
        }

    }

    /**
     * 解析图片分页URL
     * @param url
     */
    public static void parsetImgPageInfo(String url,String saveDirectory) throws IOException {
        try {
            Document document = Jsoup.connect(url).get();
            Elements content = document.getElementsByClass("content");
            //获取分页信息
            document.select("#pages a").forEach(element -> {
                //分页链接地址url,格式：/item/21027_2.html
                String pageUrl = element.attr("href");
                if(url!=null){
                    //保存图片
                    try {
                        parseImgUrl(pageUrl,saveDirectory);
                    } catch (IOException e) {
                        e.printStackTrace();
                        System.out.println("保存图片异常，地址："+pageUrl);
                    }
                }
            });
        }catch (Exception e){
            System.out.println("异常"+url);
        }
    }

    /**
     * 解析出图片的url
     * @param pageUrl
     * @throws IOException
     */
    private static void parseImgUrl(String pageUrl,String saveDirctory) throws IOException {
        if(pageUrl==null || pageUrl==""){
            return;
        }
        Document document = Jsoup.connect(WEBURL + pageUrl).get();
        //解析图片地址
        document.select(".content img").forEach(element -> {
            String imgUrl = element.attr("src");
            String title = element.attr("alt");
            //保存文件
            try {
                saveImg(imgUrl,title,saveDirctory);
            } catch (IOException e) {
                System.out.println(imgUrl+"保存失败");
                failCount++;
                e.printStackTrace();
            }
        });
    }

    /**
     * 保存图片到本地
     * @param imgUrl
     * @param title
     * @param saveDirctory
     * @throws IOException
     */
    private static void saveImg(String imgUrl, String title,String saveDirctory) throws IOException {
        HttpGet httpGet = new HttpGet(imgUrl);
        CloseableHttpResponse execute = HttpClients.createDefault().execute(httpGet);
        InputStream content = execute.getEntity().getContent();
        File file = new File(rootPath+ File.separator+saveDirctory+File.separator
                + UUID.randomUUID().toString().replace("-","").substring(0,5)+title+".jpg");
        if(file.getParentFile()==null|| !file.getParentFile().isDirectory()){
            file.getParentFile().mkdirs();
        }
        FileOutputStream fileOutputStream =
                new FileOutputStream(file);

        byte[] bytes = new byte[2048];
        int flag = 0;
        while ((flag=content.read(bytes))!=-1){
            fileOutputStream.write(bytes,0,flag);
        }
        fileOutputStream.flush();
        content.close();
        fileOutputStream.close();
        successCount++;
    }

}
